package com.zzw.system.controller;

import org.springframework.security.access.prepost.PreAuthorize;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zzw.common.result.Result;
import com.zzw.common.utils.MD5;
import com.zzw.model.system.SysUser;
import com.zzw.vo.system.SysUserQueryVo;
import com.zzw.system.service.SysUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 用户表 前端控制器
 * </p>
 *
 * @author zzw
 * @since 2023-04-20
 */
@Api(tags = "用户管理")
@RestController
@RequestMapping("/admin/system/sysUser")
public class SysUserController {
    @Autowired
    private SysUserService sysUserService;


    @PreAuthorize("hasAuthority('bnt.sysUser.remove')")
    @ApiOperation(value = "删除用户")
    @DeleteMapping("removeUser/{id}")
    public Result removeUser(@PathVariable Long id) {
        if (sysUserService.removeById(id)) {
            return Result.ok();
        }
        return Result.fail();
    }


    @PreAuthorize("hasAuthority('bnt.sysUser.add')")
    @ApiOperation("添加用户")
    @PostMapping("saveUser")
    public Result saveUser(@RequestBody SysUser sysUser) {
//        用户密码MD5加密
        String Md5Password = MD5.encrypt(sysUser.getPassword());
        sysUser.setPassword(Md5Password);
        if (sysUserService.save(sysUser)) {
            return Result.ok();
        }
        return Result.fail();
    }


    //    修改用户的步骤
//    1.根据id查找用户
    @PreAuthorize("hasAuthority('bnt.sysUser.list')")
    @ApiOperation("根据id查找用户")
    @PostMapping("fidUserById/{id}")
    public Result<SysUser> fidUserById(@PathVariable Long id) {
        return Result.ok(sysUserService.getById(id));
    }

    //    2.最终修改
    @PreAuthorize("hasAuthority('bnt.sysUser.update')")
    @ApiOperation("修改用户")
    @PutMapping("updateUser")
    public Result updateUser(@RequestBody SysUser sysUser) {
        String Md5Password = MD5.encrypt(sysUser.getPassword());
        sysUser.setPassword(Md5Password);
        if (sysUserService.updateById(sysUser)) {
            return Result.ok();
        }
        return Result.fail();
    }
    //    @RequestBody传入为json格式
    //    json格式中的数组对于java的list集合
    @PreAuthorize("hasAuthority('bnt.sysUser.remove')")
    @ApiOperation("批量删除")
    @DeleteMapping("batchRemove")
    public Result batchRemove(@RequestBody List<Long> ids) {
        if (sysUserService.removeByIds(ids)) {

            return Result.ok();
        }
        return Result.fail();
    }

    @PreAuthorize("hasAuthority('bnt.sysUser.list')")
    @ApiOperation("获取分页列表")
    @GetMapping("{page}/{limit}")
    public Result findPageQueryUser(
            @ApiParam(name = "page", value = "当前页码", required = true)
            @PathVariable Long page,
            @ApiParam(name = "limit",value = "每页记录数",required = true)
            @PathVariable Long limit,
            @ApiParam(name = "UserQueryVo", value = "查询对象", required = false)
            SysUserQueryVo UserQueryVo
                                             ){
        Page<SysUser> pageParam=new Page<>(page,limit);
       IPage<SysUser> pageMode= sysUserService.selectPage(pageParam,UserQueryVo);
        return Result.ok(pageMode);
    }


//    status =》 1：可用； 0：禁用
@PreAuthorize("hasAuthority('bnt.sysUser.update')")
    @ApiOperation("更改用户状态")
    @GetMapping("updateStatus/{id}/{status}")
    public Result updateStatus(@PathVariable Long id, @PathVariable Integer status) {
        sysUserService.updateStatus(id, status);
        return Result.ok();
    }
}

